<?php
class RewardPunishment_model extends MY_Model {
	
    function RewardPunishment_model(){
        parent::MY_Model();
        $this->load->database();
        $this->table_name = "reward_punishment";
        $this->table_fields = array('reward_id','reward_nama','reward_jenis');
    }
    
    /**
     * @param $search_key
     * @param $where
     * @param $limit
     * @param $offset
     * @param $orderby
     */
    function select_last_entry($search_key=false, $where=false, $limit=10, $offset=0, $orderby=""){
    	$db = $this->db;
		
		if ($search_key){
			if (!is_array($search_key)) $search_key = array($search_key);
			
			$qq = '';
			foreach($search_key as $kk){
//				$db->like('reward_id', $kk);
//				$db->or_like('reward_nama', $kk);
//				$db->or_like('reward_jenis', $kk);	
//				$db->where("(
//					reward_id LIKE ". $this->db->escape("%$kk%") ." OR 
//					reward_nama LIKE ".$this->db->escape("%$kk%") ." OR
//					reward_jenis LIKE ".$this->db->escape("%$kk%").")");
				foreach($this->table_fields as $ff){
					$qq .= ($qq? " OR " : "")."$ff LIKE ". $this->db->escape("%$kk%"); 
				}
			}
			$db->where("($qq)");
		}
		
		if ($where) $db->where($where);
		
    	if ($orderby) $db->order_by($orderby);
		
		$db->limit($limit, $offset);
		
		return $db->get($this->table_name);
    }
        
    function select_by_id($id, $orderby=false){
    	if ($orderby) $this->db->order_by($orderby);
    	return $query = $this->db->get_where($this->table_name, array('reward_id' => $id));
    }
}